Skip to content

feat(chameleon): Chameleon-7B Thor FP8 and Orin SM87 INT8/QuaRot frontends - #166

Open
DXICM wants to merge 7 commits into
flashrt-project:mainfrom
DXICM:feat/chameleon-model
Open

feat(chameleon): Chameleon-7B Thor FP8 and Orin SM87 INT8/QuaRot frontends#166
DXICM wants to merge 7 commits into
flashrt-project:mainfrom
DXICM:feat/chameleon-model

Conversation

@DXICM

@DXICM DXICM commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds standalone Chameleon-7B image/text VLM frontends for Jetson Thor SM110 and Jetson Orin SM87. This PR depends on #165 and must be merged after it.

The branch has been rebased onto current main and now contains only the model-layer delta: no csrc/ or CMake files from #165 are included.

Frontends

License boundary

The Research-License Meta VQGAN implementation, license, and notice were removed from the package.

Both Thor eager tokenization and the TensorRT builder now use the Apache-2.0 Transformers ChameleonVQVAE implementation and load only model.vqmodel.* tensors from the user-provided safetensors checkpoint. Loading is strict, shard-selective, and covered by a synthetic checkpoint test. The adapter supports both the Transformers 4.43 tuple output and newer image_tokens model output.

A chameleon optional dependency group now installs the required Torch, safetensors, Transformers, and Pillow runtime dependencies.

Runtime contracts

  • Thor rejects max_seq < 16 before allocation; pad-to-16 can no longer create a zero-capacity runtime.
  • Contract tests only skip exact missing opt-in native modules in Chameleon-OFF source-tree runs.
  • Chameleon-ON builds re-raise missing module errors, undefined symbols, dependency errors, and other frontend import failures.
  • Optional flash_rt_fp4 handling catches only the exact missing module, not arbitrary import/link errors.

Validation

  • Chameleon-ON SM110 build artifacts, frontend/import/API/max-seq contracts: 24 passed.
  • Apache Transformers VQ-VAE strict loader and compatibility tests: 3 passed.
  • Combined install, generic model-loading, registry, frontend, and Chameleon tests: 93 passed.
  • All changed Python files compile.
  • git diff --check and pyproject.toml parsing pass.
  • Changed-file scan contains no private paths or restricted VQGAN source/license text.
  • Current diff contains no csrc/ or CMake changes.

Merge order

  1. Merge feat(csrc): Chameleon-7B kernel layer — fused quant, INT8 GEMMs, FA2 causal attention #165.
  2. Refresh this PR against the resulting main if GitHub reports any new base movement.
  3. Run the final SM110 and SM87 device-level model smoke tests.
  4. Merge feat(chameleon): Chameleon-7B Thor FP8 and Orin SM87 INT8/QuaRot frontends #166.

@DXICM
DXICM requested a review from LiangSu8899 as a code owner August 6, 2026 05:55
@LiangSu8899

LiangSu8899 commented Aug 6, 2026

Copy link
Copy Markdown
Member

Thank you for contributing the complete Chameleon-7B Thor FP8 and Orin SM87 frontends. The responsibilities of the model layer, pipelines, and hardware backends are generally separated clearly, and we did not find an existing model being redirected to the Chameleon runtime. Codex reviewed this PR against FlashRT's long-term maintenance standards. We recommend addressing the following items after #165 has been corrected and merged.

Required changes

  1. Resolve the dependency and rebase first

    This PR currently contains all commits from feat(csrc): Chameleon-7B kernel layer — fused quant, INT8 GEMMs, FA2 causal attention #165 and conflicts with the latest main in flash_rt/api.py. Please merge feat(csrc): Chameleon-7B kernel layer — fused quant, INT8 GEMMs, FA2 causal attention #165 after its model-level build isolation is complete, then rebase this PR onto the new main, remove the duplicated kernel commits, and resolve the API registry conflict.

  2. Fix the Thor prompt-padding capacity boundary

    set_prompt() currently checks the unpadded prompt length against max_seq and pads it to a multiple of 16 afterward. If max_seq is not a multiple of 16, the padded sequence can exceed the allocated buffers and KV-cache capacity.

    Please compute and validate padded_len <= self._Se_max, or normalize the effective capacity during initialization. Add boundary tests covering non-aligned max_seq values and prompts close to capacity.

  3. Add an SM110 fail-fast check to the Thor frontend

    The Orin frontend explicitly validates SM87, but the Thor frontend has no corresponding capability check. Please call torch.cuda.get_device_capability() before loading the checkpoint, allocating large buffers, or loading hardware-specific libraries, and report a clear error on non-SM110 devices. If a development override is necessary, it should use a documented, Chameleon-specific environment variable.

  4. Fix the Orin generation-parameter boundary

    With max_new_tokens <= 0, the current implementation may still run prefill and return one token. Please define the behavior explicitly: reject negative values with ValueError, and return an empty generated result for zero. The sequence-capacity boundary should be covered by tests as well.

  5. Complete the license handling for the vendored VQGAN code

    The vendored source headers state that the files are governed by the Chameleon License, while the repository currently contains only its Apache-2.0 root license and does not include the corresponding third-party license or NOTICE. Please verify the actual source and applicable license:

    • If the code comes from Meta Chameleon, include the complete license, attribution, and modification notice, and confirm that its restrictions are compatible with this repository's distribution policy.
    • If the code comes from the original MIT-licensed CompVis implementation, derive the required code from that source and retain its copyright and MIT license attribution.

    The vendored files should not be released until third-party license compatibility is confirmed.

  6. Add contract tests proportionate to the PR's scope

    The current new tests mainly inspect two constructor defaults, which is not enough to protect two substantial frontends and pipelines. At minimum, please cover registry and lazy import behavior, optional dependencies, configuration fail-fast behavior, prompt and generation boundaries, missing-backend errors, and basic eager-versus-graph consistency.

Pre-merge checklist

  • feat(csrc): Chameleon-7B kernel layer — fused quant, INT8 GEMMs, FA2 causal attention #165 is merged, and this PR is rebased onto the latest main without duplicated kernel commits
  • GitHub reports the PR as mergeable, with no flash_rt/api.py conflict
  • With Chameleon disabled, existing models retain their default build and runtime behavior
  • Thor rejects non-SM110 devices early, and Orin rejects non-SM87 devices early
  • Thor padding and max_seq boundary tests pass
  • Negative, zero, and capacity-clipped max_new_tokens behavior is tested
  • Import, registry, and optional-dependency smoke tests pass
  • SM87 and SM110 each pass model-selected build, import, prefill, and decode smoke tests
  • Vendored VQGAN licensing and attribution are complete
  • git diff --check, Python compilation, and relevant tests pass

Please also refer to the repository's PR Review Checklist and Adding a New Model guides.

This is a Codex-assisted maintainability review. The architecture is moving in a reasonable direction; the dependency order, model build isolation, input boundaries, and third-party distribution requirements need to be completed before these hardware frontends are suitable for long-term support on main.

DXICM pushed a commit to DXICM/FlashRT that referenced this pull request Aug 7, 2026
…SM110 fail-fast,

max_new_tokens contract, VQGAN license, contract tests

Address the flashrt-project#166 maintainability review:

- Thor prompt-pad boundary: allocation floors capacity to a multiple of
  16 and set_prompt validates the PADDED length, so a non-aligned
  max_seq can never let pad-to-16 overshoot the buffers/KV cache.
- Thor hardware gate: ChameleonTorchFrontendThor checks device
  capability before checkpoint load / CUDA allocation; documented dev
  override FLASHRT_CHAMELEON_THOR_FORCE=1.
- Generation boundary: Thor generate_greedy and Orin generate both
  reject negative max_new_tokens (ValueError); Orin returns an empty
  result for zero instead of running prefill and emitting one token.
- VQGAN licensing: vendored Meta Chameleon files now carry the full
  Chameleon Research License (LICENSE) plus a NOTICE recording
  provenance (incl. the upstream CompVis MIT attribution), the
  inference-only modifications, and a compatibility notice. Documented
  in chameleon_usage.md.
- Contract tests (tests/test_chameleon_contracts.py): registry +
  lazy-import, load_model chameleon redirect, Thor/Orin hardware
  fail-fast, prompt padding bounds, and generation-parameter bounds.
@DXICM
DXICM force-pushed the feat/chameleon-model branch from a57db4c to 193797e Compare August 7, 2026 08:39
@DXICM

DXICM commented Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. The branch has been rebased onto the corrected #165 head and all six items are addressed in 193797e; once #165 merges, this PR will be rebased onto main with the duplicated kernel commits dropped and the flash_rt/api.py conflict resolved. Item-by-item:

1. Dependency/rebase — rebased onto the corrected #165 head; will rebase onto main immediately after #165 merges.

2. Thor prompt-padding capacity boundary — capacity is now floored to a multiple of 16 at allocation (_allocate_buffers), and set_prompt validates the padded length against that capacity, so a non-aligned max_seq can never let the pad-to-16 overshoot the buffers or KV cache. Covered by tests/test_chameleon_contracts.py: non-aligned-capacity overshoot raises, in-capacity prompts pad correctly, and an exact multiple-of-16 prompt passes without padding.

3. SM110 fail-fastChameleonTorchFrontendThor now calls torch.cuda.get_device_capability() before checkpoint loading, buffer allocation, and hardware-specific library loading, raising a clear RuntimeError on non-SM110 devices. The dev override is the documented, Chameleon-specific FLASHRT_CHAMELEON_THOR_FORCE=1. Covered by mocked-CUDA tests (rejects no-CUDA and wrong capability, accepts SM110, honors the override) mirroring the Orin frontend's existing gate.

4. Orin generation-parameter boundarygenerate() now defines the behavior explicitly: negative max_new_tokens raises ValueError, zero returns an empty result with no prefill/decode (previously it ran prefill and returned one token), and over-capacity values are clipped with a warning. Thor generate_greedy got the same negative-value rejection. Covered by tests; the sequence-capacity clip warning is exercised by the existing boundary.

5. VQGAN licensing — the vendored files are from Meta Chameleon (chameleon/vae/), whose core vqgan.py derives from the MIT-licensed CompVis taming-transformers. I've added the complete Chameleon Research License text (flash_rt/models/chameleon/vqgan/LICENSE) and a NOTICE with provenance (including the preserved CompVis MIT attribution), the inference-only modification record, and a compatibility notice. One point needs a maintainer policy decision: the Chameleon Research License is noncommercial-research-only, which is more restrictive than this repository's Apache-2.0. I have not re-derived the files from the MIT CompVis source because that would alter production-validated code. If the project's distribution policy can't accept a noncommercial component, the options are (a) re-derive from the MIT CompVis implementation, or (b) substitute an independently-licensed VQ-GAN. I did not remove the files pending that decision; please advise.

6. Contract teststests/test_chameleon_contracts.py adds: registry map + lazy-module-string checks, resolve_pipeline_class resolution for both hardware targets, the SM87 allowlist rejecting unsupported configs, the load_model(config="chameleon") redirect, both hardware fail-fast gates, prompt padding bounds, and generation-parameter bounds. Eager-versus-graph consistency is covered by the existing precision harness (scripts/check_chameleon_thor_precision.py) which requires a real checkpoint; happy to convert it to a checkpoint-gated pytest gate if you prefer it in-tree.

Production Orin re-test of this branch (build matrix + contract tests) is queued; results will be reported here.

@DXICM

DXICM commented Aug 8, 2026

Copy link
Copy Markdown
Contributor Author

Production re-test of 193797e on Jetson Orin (sm_87, CUDA 12.2, torch 2.3.0) — all green:

  • Build/symbol matrix correct after rebase onto the corrected feat(csrc): Chameleon-7B kernel layer — fused quant, INT8 GEMMs, FA2 causal attention #165 (Chameleon OFF default surface unchanged; ON build/import/symbol checks pass).
  • tests/test_chameleon_contracts.py passes: registry/lazy-import, load_model(config="chameleon") redirect, SM87 allowlist, Orin hardware fail-fast and env override, prompt pad-to-16 capacity boundaries (non-aligned overshoot rejection), and max_new_tokens negative/zero behavior.
  • 0 failures; all skips are documented hardware-capability gates (Thor-only kernels, sm_89+ FP8).

Outstanding: the VQGAN license policy decision flagged above, and the #165 merge + rebase onto main.

DXICM and others added 7 commits August 10, 2026 14:00
Standalone Chameleon-7B (image+text) prefill/decode frontend for Jetson
AGX Thor:

- flash_rt/models/chameleon/pipeline_thor.py: 32-layer Chameleon forward
  with runtime dynamic per-tensor FP8 (fused quantize kernels), cuBLASLt
  per-shape autotune, selective L31 ffn_down clamp, optional AWQ V-proj
  and NVFP4 FFN tiers, CUDA-graph capture with re-embed before replay,
  and incremental KV-cache decode over fmha_fp16_causal_br.
- Vendored Meta Chameleon VQ-GAN tokenizer (flash_rt/models/chameleon/vqgan,
  Meta Chameleon License headers retained; see the package docstring) with
  an eager default path and an opt-in TensorRT engine backend
  (hardware/thor/vqgan_trt_backend.py).
- ChameleonTorchFrontendThor (frontends/torch/chameleon_thor.py):
  checkpoint_dir is a required argument with a clear error when missing;
  declarative weight spec in _chameleon_thor_spec.py.
- hardware/thor/attn_backend_chameleon.py: CUTLASS causal FMHA backend
  with optional FA4 fast path, loading libfmha_fp16_causal.so from the
  package directory.
Jetson AGX Orin (SM87) Chameleon-7B path aligned with the upstream
rtx_sm87 naming:

- flash_rt/models/chameleon/pipeline_rtx.py: one chameleon_forward
  serving prefill and decode on the SM80 CUTLASS INT8/INT4 rowwise GEMMs
  with QuaRot-Hadamard rotations (correctness requirement, not an
  optimization) and the ffn_down clamp on the last 4 layers (FP16 65504
  overflow guard).
- _chameleon_quant.py: INT8/INT4 weight quantization + Hadamard packing
  from the BF16 checkpoint.
- _chameleon_spec.py: declarative weight spec with an inlined,
  Chameleon-specific _llm_block (no bias terms, no FP8 scales).
- ChameleonTorchFrontendRtxSm87 (chameleon_rtx_sm87.py): set_prompt /
  prefill / decode_step / generate, FLASHRT_CHAMELEON_SM87_FORCE escape
  hatch.
- hardware/rtx/attn_backend_chameleon.py: FA2 fwd_fp16_causal is
  mandatory for decode (bottom-right causal semantics); the backend
  raises rather than falling back to a top-left cuBLAS mask, which would
  be silently wrong.

Runtime numbers (21.07 tok/s, 16/16 bit-identical greedy vs HF BF16)
were measured on Orin hardware in the derivative repo and still need
SM87 validation here.
- Register ("chameleon", "torch", "thor") and ("chameleon", "torch",
  "rtx_sm87") in _PIPELINE_MAP and allow the SM87 key in _SM87_ALLOWED.
- api.load_model redirect for config="chameleon" (chat-style VLM, same
  pattern as qwen3_vl): raises NotImplementedError pointing at the two
  direct-instantiation frontends.
- tests/test_chameleon_thor_vqgan_backend.py: eager-vs-TRT VQGAN backend
  contract test.
- scripts/: bench_chameleon_thor.py, check_chameleon_thor_precision.py,
  profile_chameleon_thor.py, chameleon_orin_check.py (Gate-1 harness) and
  build_vqgan_trt.py (now driven by the vendored
  flash_rt.models.chameleon.vqgan package); HF BF16 reference rows use
  transformers' ChameleonForConditionalGeneration directly.
- examples/thor/chameleon_quickstart.py + README entry,
  benchmarks/chameleon_thor_latency.py.
- Docs: chameleon_usage.md, chameleon_thor_sm110.md and
  chameleon7b_rtx_sm87.md; Chameleon rows in USAGE.md, README.md and
  docs/benchmark_comparison.md.

Thor numbers were measured on Jetson AGX Thor (sm_110). All SM87 runtime
numbers in the Orin doc (21.07 tok/s, 16/16 bit-identical greedy vs HF
BF16) come from Orin hardware in the derivative repo and still need SM87
validation in this tree.
- FLASHRT_RYNNVLA2_FP4_LAYERS -> FLASHRT_CHAMELEON_FP4_LAYERS (the env
  var was inherited from the RynnVLA port with its old name)
- replace "001"/"002"/"vendor bf16" comments with plain Chameleon /
  HF-reference wording in pipeline_thor.py and chameleon_thor.py
- Add #!/usr/bin/env python3 shebangs to 4 scripts and 1 benchmark
- Rename _chameleon_spec.py to _chameleon_rtx_sm87_spec.py (hardware
  suffix per adding_new_model.md convention) and update the import
- Add config="chameleon" to docs/stable_api.md (config enum, redirect
  bullet, resolve_pipeline_class registration)
- Translate docs/chameleon_thor_sm110.md from Chinese to English
- Remove all internal "derivative repo" / "RynnVLA" provenance
  references from both engineering docs (42 occurrences)
- Move fp4_chameleon_layer16 benchmark from tests/ to benchmarks/
  (it has no test_ functions; misfiled in the kernels branch)
…SM110 fail-fast,

max_new_tokens contract, VQGAN license, contract tests

Address the flashrt-project#166 maintainability review:

- Thor prompt-pad boundary: allocation floors capacity to a multiple of
  16 and set_prompt validates the PADDED length, so a non-aligned
  max_seq can never let pad-to-16 overshoot the buffers/KV cache.
- Thor hardware gate: ChameleonTorchFrontendThor checks device
  capability before checkpoint load / CUDA allocation; documented dev
  override FLASHRT_CHAMELEON_THOR_FORCE=1.
- Generation boundary: Thor generate_greedy and Orin generate both
  reject negative max_new_tokens (ValueError); Orin returns an empty
  result for zero instead of running prefill and emitting one token.
- VQGAN licensing: vendored Meta Chameleon files now carry the full
  Chameleon Research License (LICENSE) plus a NOTICE recording
  provenance (incl. the upstream CompVis MIT attribution), the
  inference-only modifications, and a compatibility notice. Documented
  in chameleon_usage.md.
- Contract tests (tests/test_chameleon_contracts.py): registry +
  lazy-import, load_model chameleon redirect, Thor/Orin hardware
  fail-fast, prompt padding bounds, and generation-parameter bounds.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants